package com.code9xs.controller;

import com.alibaba.fastjson.JSONObject;
import com.code9xs.pojo.Role;
import com.code9xs.pojo.User;
import com.code9xs.util.PageUtil;
import com.code9xs.util.Result;
import lombok.val;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author Suhd
 * @date 2021/4/25
 * @time 13:25
 * @wechat i9xswanan
 * @github https://github.com/Code9xs
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController{
    @PostMapping("/reg")
    public Result reg(@RequestBody JSONObject param){
        boolean flag = userService.reg(param);
        if (flag){
            return Result.succ(200,"注册成功","");
        }
        return Result.fail("邮箱已注册");
    }

    @PostMapping("/association")
    public Result joinAssociation(@RequestBody JSONObject param){
        boolean flag = userService.joinAssociation(param);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"提交成功","");
        }
        return Result.fail(HttpServletResponse.SC_PRECONDITION_FAILED,"提交失败,用户已存在该社团","");
    }

    @GetMapping("/info")
    public Result getUserInfo(){
        return Result.succ(HttpServletResponse.SC_OK,"成功",userService.getCurrentUser());
    }

    @GetMapping("/personal")
    public Result getUserPersonalCenter(){
        return Result.succ(HttpServletResponse.SC_OK,"成功",userService.getUserPersonalCenter());
    }

    @PostMapping("/modify/userInfo")
    public Result  modifyUserInfo(@RequestBody JSONObject param){
        User user = new User();
        user.setId(param.getInteger("id"));
        user.setUsername(param.getString("username"));
        user.setAvatar(param.getString("avatar"));
        user.setPhone(param.getString("phone"));
        user.setGender(param.getString("gender"));
        boolean flag = userService.updateUserInfo(user);
        if(flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.succ(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @GetMapping("/current")
    public Result getCurrentUser(){
        return Result.succ(HttpServletResponse.SC_OK,"成功",SecurityContextHolder.getContext().getAuthentication());
    }

    @GetMapping("/modifyMail/checkCode")
    public Result getMailCheckCode(){
        return Result.succ(HttpServletResponse.SC_OK,"成功",userService.getMailCheckCode());
    }

    @PostMapping("/modifyPd")
    public Result modifyMail(@RequestBody JSONObject param){
        final String newPassword = param.getString("new_pd");
        final String key = param.getString("key");
        final String checkCoke = param.getString("checkCode");
        return userService.modifyPassword(newPassword,key,checkCoke);
    }

    @GetMapping("/roles")
    public Result getCurrentUserRoles(){
        List<Map<String,Object>> roles = userService.getCurrentUserRoles();
        return Result.succ(HttpServletResponse.SC_OK,"成功",roles);
    }

    @GetMapping("/todo")
    public Result getUserToDo(@RequestParam("page")String page,@RequestParam("pageSize")String pageSize){
        PageUtil pageUtil = userService.getUserTodo(Integer.valueOf(page), Integer.valueOf(pageSize));
        return Result.succ(HttpServletResponse.SC_OK,"成功",pageUtil);
    }

    @GetMapping("/todo/count")
    public Result getAllToDoCount(){
        return Result.succ(HttpServletResponse.SC_OK,"成功",userService.getAllToDoCount());
    }

}
